//
//  ChangeViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/28.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "ChangeViewController.h"

@interface ChangeViewController ()<UITextFieldDelegate>
//返回按钮
@property (nonatomic, retain) UIButton *backButton;
//用户
@property (nonatomic, retain) UITextField *userField;
@property (nonatomic, retain) UILabel *userLabel;
//原密码
@property (nonatomic, retain) UITextField *oldField;
@property (nonatomic, retain) UILabel *oldLabel;
//新密码
@property (nonatomic, retain) UITextField *theNewField;
@property (nonatomic, retain) UILabel *theNewLabel;
//确定
@property (nonatomic, retain) UIButton *button;

@end

@implementation ChangeViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.87 green:0.86 blue:0.87 alpha:1.00];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(5, 10, 50, 50);
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"<返回" forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    
    
    //用户名
    self.userField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 90, self.view.frame.size.height / 2 - 150, 200, 30)];
    _userField.layer.cornerRadius = 15;
    _userField.layer.borderWidth = 1;
    _userField.delegate = self;
    _userField.placeholder = @"请输入用户名";
    _userField.layer.borderColor = [UIColor grayColor].CGColor;
    _userField.text = _str;
//    [self.view addSubview:_userField];
//    
//    self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 160, self.view.frame.size.height / 2 - 150, self.view.frame.size.width / 2 - 100, 30)];
//    _userLabel.text = @"用户名:";
//    _userLabel.font = [UIFont systemFontOfSize:17];
//    [self.view addSubview:_userLabel];
//    
    
    
    
    //旧密码
    self.oldField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 90, self.view.frame.size.height / 2 - 100, 200, 30)];
    _oldField.layer.cornerRadius = 15;
    _oldField.layer.borderWidth = 1;
    _oldField.delegate = self;
    _oldField.secureTextEntry = YES;
    _oldField.placeholder = @"请输入旧密码";
    _oldField.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:_oldField];

    self.oldLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 160, self.view.frame.size.height / 2 - 100, self.view.frame.size.width / 2 - 100, 30)];
    _oldLabel.text = @"旧密码:";
    _oldLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_oldLabel];
    
    
    
    //新密码
    self.theNewField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 90, self.view.frame.size.height / 2 - 50, 200, 30)];
    _theNewField.layer.cornerRadius = 15;
    _theNewField.layer.borderWidth = 1;
    _theNewField.delegate = self;
    _theNewField.secureTextEntry = YES;
    _theNewField.placeholder = @"请输入新密码";
    _theNewField.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:_theNewField];
    
    self.theNewLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 160, self.view.frame.size.height / 2 - 50, self.view.frame.size.width / 2 - 100, 30)];
    _theNewLabel.text = @"新密码:";
    _theNewLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_theNewLabel];
    
    
    
    
    //确定按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(self.view.frame.size.width / 2 - 25, self.view.frame.size.height / 2, 50, 50);
    [_button setTitle:@"确定" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    _button.layer.borderWidth = 1;
    _button.layer.cornerRadius = 25;
    _button.layer.borderColor = [UIColor grayColor].CGColor;
    
    
    
}

-(void)buttonAction:(UIButton *)button{
    
   // NSLog(@"1213");
    NSLog(@"%@", _userField.text);
    BmobUser *user = [BmobUser getCurrentUser];
    [user updateCurrentUserPasswordWithOldPassword:_oldField.text newPassword:_theNewField.text block:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //用新密码登录
            [BmobUser loginInbackgroundWithAccount:_userField.text andPassword:_theNewField.text block:^(BmobUser *user, NSError *error) {
                if (error) {
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改密码提示" message:@"修改密码成功但登录失败" preferredStyle:UIAlertControllerStyleAlert];
                    
                    
                    
                    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        [self dismissViewControllerAnimated:YES completion:^{
                            
                        }];
                    }];
                    
                    [alert addAction:alertAction1];
                    [self presentViewController:alert animated:YES completion:nil];

                    
                    
                    
                } else {
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改密码提示" message:@"修改密码成功" preferredStyle:UIAlertControllerStyleAlert];
                    
                    
                    
                    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        [self dismissViewControllerAnimated:YES completion:^{
                            
                        }];
                    }];
                    
                    [alert addAction:alertAction1];
                    [self presentViewController:alert animated:YES completion:nil];

                    
                    
                }
            }];
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改密码提示" message:@"请先登录在修改密码" preferredStyle:UIAlertControllerStyleAlert];
            
            
            
            UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
            
            [alert addAction:alertAction1];
            [self presentViewController:alert animated:YES completion:nil];
            
            
            
        }
    }];
}

-(void)backAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
